<template>
  <div class="white-box">
    <div style="margin-bottom: 20px">
      <el-text type="danger" style="margin-left: 5px">注意：不能小于0，2位小数，单位%</el-text>
    </div>


    <el-form ref="formRef" :model="formData" label-width="120px">
      <el-row>
        <el-col :span="8" v-for="(item,index) in formData" :key="item.money_config_id">
          <el-form-item :label="'【'+list[index].user_type_label + '】' + list[index].house_type_label" :prop="index + '.money_ratio'" required :rules="formRule">
            <el-input v-model.trim ="item.money_ratio" clearable placeholder="请输入佣金比例" oninput="value=value.replace(/[^\d.]/g,'')"/>

          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <div class="foot-btn flexs-center" style="margin-top: 30px">
          <el-button type="primary" size="large" :loading="btnLoading" @click="submitForm(formRef)">确认修改</el-button>
        </div>
      </el-form-item>
    </el-form>

    <go-back/>
  </div>
</template>

<script setup>
import {getCurrentInstance, ref, onMounted, shallowRef, reactive, onBeforeUnmount} from "vue"
import {useRouter, useRoute} from "vue-router"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const route = useRoute()
const {proxy} = getCurrentInstance()

onMounted(() => {
  getDetail()
})
const btnLoading = ref(false)
const formRef = ref(null)
const formData = reactive([])
const list = reactive([])

const formRule = [
  {
    validator:(rule,value,callback)=>{
      if(value == ''){
        callback(new Error('请输入数字'))
      }else if(/^\d+(\.\d{1,2})?$/.test(value)){
        callback()
      }else{
        callback(new Error('佣金比例最多2位小数'))
      }
    },
    rigger: 'blur',
  }
]
const getDetail = () => {
  proxy.$axios
      .get("admin/finance/config/detail")
      .then((res) => {
        if (res.data.code == 200) {
          let result = res.data.data
          result.forEach(v=>{
            list.push(v);
            formData.push({
              money_ratio:v.money_ratio,
              money_config_id:v.money_config_id
            })
          })
        } else {
          proxy.$message({message: res.data.msg, type: "error"})
        }
      })
      .catch((err) => {
      })
}
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
      let url = "admin/finance/config/update"
      btnLoading.value = true
      proxy.$axios
          .post(url,{formData:formData})
          .then((res) => {
            btnLoading.value = false
            if (res.data.code == 200) {
              proxy.$message({message: "保存成功！", type: "success"})
            } else {
              proxy.$message({message: res.data.msg, type: "error"})
            }
          })
          .catch((err) => {
            btnLoading.value = false
          })
    }
  })
}
</script>

<style lang="less" scoped>

</style>
